const express = require("express");
const Delivery = require("../db/models/delivery");
const Package = require("../db/models/package");

module.exports = {
  getAllDeliveries(req, res) {
    Delivery.findAll()
      .then((data) => {
        res.json({
          code: 200,
          message: "获取成功",
          data: data,
        });
      })
      .catch((err) => {
        res.status(500).json({
          code: 500,
          message: "获取失败",
          error: err,
        });
      });
  },

  getDelivery(req, res) {
    Delivery.findById(req.params.id)
      .then((data) => {
        if (data == null) {
          return res.status(404).json({
            code: 404,
            message: "未找到配送",
          });
        }
        res.json({
          code: 200,
          message: "获取成功",
          data: data,
        });
      })
      .catch((err) => {
        res.status(500).json({
          code: 500,
          message: "获取失败",
          error: err,
        });
      });
  },

  createDelivery(req, res) {
    const delivery = new Delivery({
      name: req.body.name,
      shelve_id: req.body.shelve_id,
    });

    delivery
      .save()
      .then((data) => {
        res.status(201).json({
          code: 201,
          message: "创建成功",
          data: data,
        });
      })
      .catch((err) => {
        res.status(400).json({
          code: 400,
          message: "创建失败",
          error: err,
        });
      });
  },

  updateDelivery(req, res) {
    const id = req.params.id;
    const { name } = req.body;
    Delivery.update(
      { name: name },
      {
        where: { id: id },
      }
    )
      .then((data) => {
        res.json({
          code: 200,
          message: "更新成功",
          data: data,
        });
      })
      .catch((err) => {
        res.status(500).json({
          code: 500,
          message: "更新失败",
          error: err,
        });
      });
  },

  deleteDelivery(req, res) {
    const id = req.params.id;
    Delivery.destroy({
      where: { id: id },
    })
      .then((data) => {
        res.json({
          code: 200,
          message: "删除成功",
          data: data,
        });
      })
      .catch((err) => {
        res.status(500).json({
          code: 500,
          message: "删除失败",
          error: err,
        });
      });
  },

  // 绑定快递
  bindPackage(req, res) {
    const delivery_id = req.params.id;
    // 获取快递信息
    const { station_id, phone_number_front, phone_number_after, name, status } =
      req.body;
    if (!station_id || !phone_number_front || !phone_number_after || !name) {
      return res.status(400).json({
        code: 400,
        message: "参数错误",
      });
    }
    // 新建快递
    const package = new Package({
      phone_number_front: phone_number_front,
      phone_number_after: phone_number_after,
      name: name,
      station_id: station_id,
      delivery_id: delivery_id,
      status: status || "未取件",
    });
    package
      .save()
      .then((data) => {
        res.json({
          code: 200,
          message: "绑定成功",
          data: data,
        });
      })
      .catch((err) => {
        res.status(400).json({
          code: 400,
          message: "绑定失败",
          error: err,
        });
      });
  },
  // 修改绑定
  updatePackage(req, res) {
    const package_id = req.params.id;
    const { station_id, phone_number_front, phone_number_after, name, status } =
      req.body;
    if (!station_id || !phone_number_front || !phone_number_after || !name) {
      return res.status(400).json({
        code: 400,
        message: "参数错误",
      });
    }
    Package.update(
      {
        name: name,
        station_id,
        phone_number_front,
        phone_number_after,
        status: status || "未取件",
      },
      {
        where: { id: package_id },
      }
    )
      .then((data) => {
        res.json({
          code: 200,
          message: "更新成功",
          data: data,
        });
      })
      .catch((err) => {
        res.status(500).json({
          code: 500,
          message: "更新失败",
          error: err,
        });
      });
  },
  // 查询空快递下所有包裹
  getDeliveryPackages(req, res) {
    Package.findOne({
      where: {
        delivery_id: req.params.id,
      },
    })
      .then((data) => {
        res.json({
          code: 200,
          message: "获取成功",
          data: data,
        });
      })
      .catch((err) => {
        res.status(500).json({
          code: 500,
          message: "获取失败",
          error: err,
        });
      });
  },
};
